package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopingCartMapper;
import com.sky.service.ShopingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopingCartUserServiceImpl implements ShopingCartService {
    @Autowired
    private ShopingCartMapper shopingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShopingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //dish_id/dish_id+口味/setmeal_id
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //去数据看查询，当前用户有没有对当前商品进行过添加操作
        //根据shoppingCart去查询
        ShoppingCart dbshoppingCart = shopingCartMapper.findShopingCartByUserIdAndOptions(shoppingCart);

        if (dbshoppingCart == null) {
            //添加
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            //设置公共的属性
            LocalDateTime createTime = LocalDateTime.now();
            int number = 1;
            if (dishId != null) {
                //1、菜品处理
                Dish dish = dishMapper.findById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //2、套餐处理
                Setmeal setmeal = setmealMapper.findById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setCreateTime(createTime);
            shoppingCart.setNumber(number);
            shopingCartMapper.save(shoppingCart);

        } else {
            dbshoppingCart.setNumber(dbshoppingCart.getNumber() + 1);
            shopingCartMapper.updateNumberById(dbshoppingCart);
        }

    }

    /**
     * 获取购物车数据
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shopingCartMapper.findByUserId(userId);

    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shopingCartMapper.clean(userId);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        ShoppingCart dbshoppingCart = shopingCartMapper.findShopingCartByUserIdAndOptions(shoppingCart);

        if (dbshoppingCart.getNumber() == 1) {
            shopingCartMapper.cleanOne(dbshoppingCart.getId());
        } else {
            dbshoppingCart.setNumber(dbshoppingCart.getNumber() - 1);
            shopingCartMapper.updateNumberById(dbshoppingCart);
        }
    }
}
